using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Media;
using DevExpress.Data.Utils;
using DevExpress.Xpf.Bars;
using Prism.Commands;
using Prism.Mvvm;
using RFS.BizLogic.PaiShi.Accounting;
using RFS.BizLogic.PaiShi.Ports;
using RFS.Domain.PaiShi.Accounting;
using RFS.Domain.Sys;

namespace RFS.WPF.Views
{
    public class AccountVoucherVm : BindableBase
    {
        OrderItemBasedAccountingVoucher _o;

        public DelegateCommand<AccItemVm> CmdRemoveAccItem { set; get; }
        public DelegateCommand CmdShowAddAddYanShouDanSelectForm { set; get; }
        public DelegateCommand CmdSelectFormOK { set; get; }
        public DelegateCommand CmdSelectFormCancel { set; get; }


        public DelegateCommand CmdAddRightComplimentFormOK { set; get; }
        public DelegateCommand CmdAddRightComplimentFormCancel { set; get; }


        public DelegateCommand CmdShowAddRightCompliment { set; get; }


        public AccountVoucherVm(IOrderItemBasedAccountingVoucher obj, IConfigT config,
            IDayBasedWorkFlowNodeAccountingVoucherSetWrapper avs, IAccountingVoucherManager voucherManager1)
        {
            _o = obj as OrderItemBasedAccountingVoucher;
            _config = config;
            _voucherSetWrapper = avs;
            _voucherManager = voucherManager1;

            _o.Changed += OOnChanged;
            
            Init();
        }

        private void Init()
        {
            CmdRemoveAccItem = new DelegateCommand<AccItemVm>(DoCmdRemoveAccItem);
            CmdShowAddAddYanShouDanSelectForm = new DelegateCommand(DoCmdShowAddYanShouDanSelectForm);
            CmdShowAddRightCompliment = new DelegateCommand(DoShowAddRightComplimentForm);

            CmdSelectFormOK = new DelegateCommand(DoCmdSelectFormOK);
            CmdSelectFormCancel = new DelegateCommand(DoCmdSelectFormCancel);

            CmdAddRightComplimentFormOK = new DelegateCommand(DoCmdAddRightComplimentFormOK);
            CmdAddRightComplimentFormCancel = new DelegateCommand(DoCmdAddRightComplimentFormCancel);

            ReadData();
        }

        private void DoCmdRemoveAccItem(AccItemVm obj)
        {
            _o.Remove(obj.DO);
        }

        private void DoCmdShowAddYanShouDanSelectForm3()
        {
        }

        void ReadData()
        {
            var vms = _o.AccItems.Select(x => new AccItemVm(x));
            AccItems = new ObservableCollection<AccItemVm>(vms);


            Balance = _o.Balance.AmountCash;
            BalanceAmount = _o.Balance;
            DebitTotal = _o.DebitTotal;
            CreditTotal = _o.CreditTotal;


            SelectCandiateByAmoutCash = _config.ConfigSys.CompareModeOfCash;
            
            
            Id = _o.Id;
            var ids = $"{_o.Id}";
            Idt =  $"..{ids.Substring(Math.Max(0,ids.Length-4 ) , Math.Min(ids.Length,4) ) }";

        }

        #region Id

        private ulong _Id;

        public ulong Id
        {
            get { return _Id; }
            set
            {
                _Id = value;
                RaisePropertyChanged(nameof(Id));
            }
        }

        #endregion

        #region Idt

        private string _Idt;

        public string Idt
        {
            get { return _Idt; }
            set
            {
                _Idt = value;
                RaisePropertyChanged(nameof(Idt));
            }
        }

        #endregion

        private void DoCmdAddRightComplimentFormCancel()
        {
            ShowAddRightCompliment = false;
        }

        private void DoCmdAddRightComplimentFormOK()
        {
            ShowAddRightCompliment = false;
        }

        private void DoCmdSelectFormCancel()
        {
            ShowAddAddYanShouDanSelectForm = false;
        }

        private void DoCmdSelectFormOK()
        {
            // adthe order item into av

            var sas = _yanShouDanCandiates.Where(x => x.Selected)
                .Select(x => x.OrderItem);

            foreach (var orderItem in sas)
            {
                // var p= _priceManager.GetPrice()
                _o.AddAccItem(orderItem, Account.KCSP, "手动选择", amountCredit: new Amount(orderItem.Amount,
                    orderItem.Quantity));
            }

            ShowAddAddYanShouDanSelectForm = false;
        }

        #region DebitTotal

        private Amount _debitTotal;

        public Amount DebitTotal
        {
            get { return _debitTotal; }
            set
            {
                _debitTotal = value;
                RaisePropertyChanged(nameof(DebitTotal));
            }
        }

        #endregion

        #region CreditTotal

        private Amount _creditTotal;

        public Amount CreditTotal
        {
            get { return _creditTotal; }
            set
            {
                _creditTotal = value;
                RaisePropertyChanged(nameof(CreditTotal));
            }
        }

        #endregion


        private void OOnChanged(object sender, EventArgs e)
        {
            ReadData();
        }

        #region BanlanceAngle

        private int _BanlanceAngle = 0;

        public int BanlanceAngle
        {
            get { return _BanlanceAngle; }
            set
            {
                _BanlanceAngle = value;
                RaisePropertyChanged(nameof(BanlanceAngle));
            }
        }

        #endregion


        #region BalanceTitle

        private string _BalanceTitle;

        public string BalanceTitle
        {
            get { return _BalanceTitle; }
            set
            {
                _BalanceTitle = value;
                RaisePropertyChanged(nameof(BalanceTitle));
            }
        }

        #endregion

        #region Balance

        private decimal _balance;

        public decimal Balance
        {
            get { return _balance; }
            set
            {
                _balance = value;
                RaisePropertyChanged(nameof(Balance));
                //
                BanlanceAngle = value == 0 ? 0 : value < 0 ? -5 : 5;
                // BanlanceAngle= value==0? 0 : value <0?  -5: 5 ;
                BalanceColor = value >= 0 ? Brushes.Green : Brushes.Red;
                BalanceTitle = value == 0 ? "平" : value < 0 ? "短" : "余";
            }
        }

        #endregion


        #region BalanceAmount

        private Amount _BalanceAmount;

        public Amount BalanceAmount
        {
            get { return _BalanceAmount; }
            set
            {
                _BalanceAmount = value;
                RaisePropertyChanged(nameof(BalanceAmount));
            }
        }

        #endregion

        #region BalanceColor

        private Brush _balanceColor;

        public Brush BalanceColor
        {
            get { return _balanceColor; }
            set
            {
                _balanceColor = value;
                RaisePropertyChanged(nameof(BalanceColor));
            }
        }

        #endregion


        // public bool ShowAddRightCompliment { get; set; }

        #region ShowAddRightCompliment

        private bool _showAddRightCompliment;

        public bool ShowAddRightCompliment
        {
            get { return _showAddRightCompliment; }
            set
            {
                _showAddRightCompliment = value;
                RaisePropertyChanged(nameof(ShowAddRightCompliment));
            }
        }

        #endregion

        void DoCmdShowAddYanShouDanSelectForm()
        {
            SelectCandidates(_o);
            ShowAddAddYanShouDanSelectForm = !ShowAddAddYanShouDanSelectForm;
        }


        IDayBasedWorkFlowNodeAccountingVoucherSetWrapper _voucherSetWrapper;

        private void SelectCandidates(OrderItemBasedAccountingVoucher orderItemBasedAccountingVoucher)
        {
            var cans = _voucherSetWrapper
                .GetYSCandiatesFor(orderItemBasedAccountingVoucher,
                    _includeHistoryCandiateAlso,
                    _config.ConfigSys.CompareModeOfCash)
                .Select(x => new YsDanSelectionOptionVm(x));


            YanShouDanCandiates = new ObservableCollection<YsDanSelectionOptionVm>(cans);
        }


        #region YanShouDanCandiates

        private ObservableCollection<YsDanSelectionOptionVm> _yanShouDanCandiates =
            new ObservableCollection<YsDanSelectionOptionVm>();

        public ObservableCollection<YsDanSelectionOptionVm> YanShouDanCandiates
        {
            get { return _yanShouDanCandiates; }
            set
            {
                _yanShouDanCandiates = value;
                RaisePropertyChanged(nameof(YanShouDanCandiates));
            }
        }

        #endregion

        void DoShowAddRightComplimentForm()
        {
            ShowAddRightCompliment = !ShowAddRightCompliment;
            Balance -= 100;
        }


        #region ShowAddAddYanShouDanSelectForm

        private bool _showAddAddYanShouDanSelectForm;


        public bool ShowAddAddYanShouDanSelectForm
        {
            get { return _showAddAddYanShouDanSelectForm; }
            set
            {
                _showAddAddYanShouDanSelectForm = value;
                RaisePropertyChanged(nameof(ShowAddAddYanShouDanSelectForm));
            }
        }

        #endregion
 


        #region Name

        private string _name;
        private IConfigT _config;

        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                RaisePropertyChanged(nameof(Name));
            }
        }

        #endregion


        #region AccItems

        private ObservableCollection<AccItemVm> _accItems = new ObservableCollection<AccItemVm>();

        public ObservableCollection<AccItemVm> AccItems
        {
            get { return _accItems; }
            set
            {
                _accItems = value;
                RaisePropertyChanged(nameof(AccItems));
            }
        }

        #endregion


        #region SelectCandidatesByQuantity

        private bool _selectCandidatesByQuantity;

        public bool SelectCandidatesByQuantity
        {
            get { return _selectCandidatesByQuantity; }
            set
            {
                _selectCandidatesByQuantity = value;
                SelectCandiateByAmoutCash = !value;

                // RaisePropertyChanged(nameof(SelectCandiateByAmoutCash));
                RaisePropertyChanged(nameof(SelectCandidatesByQuantity));
            }
        }

        #endregion

        #region SelectCandiateByAmoutCash

        private bool _selectCandiateByAmoutCash;

        public bool SelectCandiateByAmoutCash
        {
            get { return _selectCandiateByAmoutCash; }
            set
            {
                _selectCandiateByAmoutCash = value;
                _selectCandidatesByQuantity = !value;
                RaisePropertyChanged(nameof(SelectCandiateByAmoutCash));
                RaisePropertyChanged(nameof(SelectCandidatesByQuantity));

                _config.ConfigSys.CompareModeOfCash = value;
                SelectCandidates(_o);
            }
        }

        #endregion


        #region IncludeHistoryCandiateAlso

        private bool _includeHistoryCandiateAlso;
        private IAccountingVoucherManager _voucherManager;
        public IOrderItemBasedAccountingVoucher DO
        {
            get => _o;
        }  

        public bool IncludeHistoryCandiateAlso
        {
            get { return _includeHistoryCandiateAlso; }
            set
            {
                _includeHistoryCandiateAlso = value;
                RaisePropertyChanged(nameof(IncludeHistoryCandiateAlso));

                SelectCandidates(_o);
            }
        }

        // public DelegateCommand CmdCheckAndCreateVouchers { get; set; }

        #endregion
    }
}